package br.ufrj.dcc.smarthotel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;

import com.google.gson.Gson;

public class ReservationActivity extends Activity {
    /** Called when the activity is first created. */
	
    String URL = "http://maps.google.es/maps/api/directions/json?origin=-22.951646,-43.211612&destination=-22.853201,-43.230822&sensor=false";  
    String result = "";  
    String deviceId = "xxxxx";
    JSONArray jsonArray;
    JSONObject jsonObject;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reservation);
        
        callWebService();

        try {
        	jsonObject = new JSONObject(result);			
			//jsonObject = jsonArray.getJSONObject(0);
			//String teste =  jsonObject.get("name").toString();
        	

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void callWebService(){
    	/*Map<String, String> kvPairs = new HashMap<String, String>();
    	JSONObject j1 = new JSONObject();
    	JSONObject j2 = new JSONObject();
        try {        	
        	j1.put("serviceId", 1);
        	j1.put("name", "diego");        	
        	j2.put("serviceId", 2);
        	j2.put("name", "patty");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		kvPairs.put("1", j1.toString());
		kvPairs.put("2", j2.toString());
		HttpResponse re;
		try {
			re = doPost(URL, kvPairs);
			String temp = EntityUtils.toString(re.getEntity());
			if (temp.compareTo("SUCCESS")==0)
			{
			    Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
			}
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		
        HttpClient httpclient = new DefaultHttpClient();  
        
        try {  
        	HttpGet request = new HttpGet(URL);
            request.addHeader("deviceId", deviceId);  
            ResponseHandler<String> handler = new BasicResponseHandler();  
            result = httpclient.execute(request, handler);  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
        httpclient.getConnectionManager().shutdown();
    }
    
    public static HttpResponse doPost(String url, Map<String, Object> kvPairs) throws ClientProtocolException, IOException 
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        
        if (kvPairs != null && kvPairs.isEmpty() == false) 
        {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(kvPairs.size());
            
            String k;
            Object v;
            
            Gson gson = new Gson();
            
            Iterator<String> itKeys = kvPairs.keySet().iterator();
            
            while (itKeys.hasNext())
            {
                k = itKeys.next();
                v = kvPairs.get(k);
                
                nameValuePairs.add(new BasicNameValuePair(k, gson.toJson(v)));
            }
            
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }
        
        HttpResponse response;
        response = httpclient.execute(httppost);
        return response;
    }

}